package com.lg.atp.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.lg.atp.common.Constants;
import com.lg.atp.entity.TblDataset;
import com.lg.atp.service.IDatasetService;
import com.lg.atp.service.IParaService;

@RestController
@RequestMapping("dataset")
public class DatasetController {

	private static Log log = LogFactory.getLog(DatasetController.class);
	@Autowired
	private IDatasetService iDatasetService;
	@Autowired
	private IParaService iParaService;
	
	@GetMapping("index")
	public ModelAndView index() {
		ModelAndView view = new ModelAndView();
		view.setViewName("datasetList");
		return view;
	}
	
	@GetMapping("toAdd")
	public ModelAndView toAdd() {
		ModelAndView view = new ModelAndView();
		view.setViewName("dataset-add");
		return view;
	}
	
	
	@GetMapping("toEdit")
	public ModelAndView toEdit(Long datasetId) {
		ModelAndView view = new ModelAndView();
		view.setViewName("dataset-edit");
		view.addObject("datasetId", datasetId);
		return view;
	}
	
	@PostMapping("list")
	public Map<String,Object> list(String datasetName,Integer pageSize,Integer pageIndex){
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("status", Constants.RETURN_SUCCESS);
		try {
			PageInfo<TblDataset> list = iDatasetService.getDatasetByPage(datasetName, pageIndex, pageSize);
			result.put("data", list);
		}catch(Exception e) {
			e.printStackTrace();
			log.error("查询数据集失败："+e.getMessage());
			result.put("status", Constants.RETURN_FAIL);
			result.put("errorMsg", "查询数据集失败");
		}
		
		return result;
	}
	
	@PostMapping("getDatasetById")
	public Map<String,Object> getDatasetById(Long datasetId){
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("status", Constants.RETURN_SUCCESS);
		try {
			TblDataset dataset = iDatasetService.getDatasetWithPara(datasetId);
			result.put("data", dataset);
		}catch(Exception e) {
			e.printStackTrace();
			log.error("获取数据集详情失败："+e.getMessage());
			result.put("status", Constants.RETURN_FAIL);
			result.put("errorMsg", "获取数据集详情失败");
		}
		
		return result;
	}
	
	
	@PostMapping("getDatasetByParaType")
	public Map<String,Object> getDatasetByParaType(Integer paraType){
		
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("status", Constants.RETURN_SUCCESS);
		try {
			TblDataset dataset = new TblDataset();
			dataset.setParaType(paraType);
			List<TblDataset> list = iDatasetService.selectDataset(dataset);
			result.put("data", list);
		}catch(Exception e) {
			e.printStackTrace();
			log.error("获取数据集失败："+e.getMessage());
			result.put("status", Constants.RETURN_FAIL);
			result.put("errorMsg", "获取数据集失败");
		}
		return result;
	}
	
	@PostMapping("checkParaName")
	public Map<String,Object> checkDatasetParaName(String paraName,Long datasetId){
		
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("status", Constants.RETURN_SUCCESS);
		try {
			Long count = iParaService.hasParaInDataset(paraName, datasetId);
			if(count != null && count > 0) {
				log.error("存在相同参数名：");
				result.put("errorMsg", "存在相同参数名");
				result.put("status", Constants.RETURN_FAIL);
			}
		}catch(Exception e) {
			e.printStackTrace();
			log.error("校验参数名失败："+e.getMessage());
			result.put("errorMsg", "校验参数名失败");
			result.put("status", Constants.RETURN_FAIL);
		}
		
		return result;
	}
	
	
	@PostMapping("saveDataset")
	public Map<String,Object> saveDataset(String str){
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("status", Constants.RETURN_SUCCESS);
		try {
			TblDataset dataset = JSONObject.parseObject(str,TblDataset.class);
			String datasetName = dataset.getDatasetName();
			String datasetCode = dataset.getDatasetCode();
			Long datasetId = dataset.getId();
			
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("datasetName", datasetName);
			if(datasetId != null)
				map.put("datasetId", datasetId);
			Long count = iDatasetService.hasSameName(map);
			if(count != null && count > 0) {
				log.error("存在相同数据集名");
				result.put("errorMsg", "存在相同数据集名");
				result.put("status", Constants.RETURN_FAIL);
				return result;
			}
			map.put("datasetCode", datasetCode);
			Long count1 = iDatasetService.hasSameCode(map);
			if(count1 != null && count1 > 0) {
				log.error("存在相同数据集编码");
				result.put("errorMsg", "存在相同数据集编码");
				result.put("status", Constants.RETURN_FAIL);
				return result;
			}
			iDatasetService.saveDataset(dataset);
		}catch(Exception e) {
			e.printStackTrace();
			log.error("保存数据集失败："+e.getMessage());
			result.put("errorMsg", "保存失败");
			result.put("status", Constants.RETURN_FAIL);
		}
		
		return result;
	}
	
}
